package com.guo.worktool.manager.factory.StrConvertFactory;

import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Service;

@Service("from")
public class FromService implements StrConvertService{

    @Override
    public String convert(String convertStr) {
        String[] split = convertStr.split("\n");
        StringBuilder sb = new StringBuilder();
        String last = split[split.length - 1];
        String blN = "";
        if(!last.contains("-")){
            blN = "formParams";
        }else {
            String[] blName = last.split("-",2);
            blN = blName[0];
        }
        int count = 0;
        String prefix = "List<NameValuePair> zdyData = new ArrayList<>();\n";
        sb.append(prefix);
        for (String str : split) {
            if(str.contains(":")){
                String[] two = str.split(":",2);
                sb.append("zdyData.add(new NameValuePair(\"").append(two[0]).append("\", ")
                        .append(JSON.toJSONString(two[1].trim())).append("));")
                        .append("\n");
                count++;
            }
        }
        sb.append("log.info(\"zdyData:{}\", zdyData);\n");
        sb.append("成功处理了").append(count).append("条");
        String returnStr = sb.toString();
        returnStr = returnStr.replace("zdyData",blN);
        return returnStr;
    }
}
